package com.yinhe.music.yhmusic.dialog.collect;

import com.apkfuns.logutils.LogUtils;
import com.hwangjr.rxbus.RxBus;
import com.yinhe.music.common.utils.JsonUtil;
import com.yinhe.music.event.RxBusEventType;
import com.yinhe.music.event.RxbusNullObject;
import com.yinhe.music.type.person.BaseInfo;
import com.yinhe.music.yhmusic.base.BaseModel;
import com.yinhe.music.yhmusic.db.DataBaseAccessor;
import com.yinhe.music.yhmusic.db.bean.MenuDBEntity;
import com.yinhe.music.yhmusic.db.bean.SongDBEntity;
import com.yinhe.music.yhmusic.http.StatusCode;
import com.yinhe.music.yhmusic.model.SongMenuList;
import com.yinhe.music.yhmusic.network.response.Response;
import com.yinhe.music.yhmusic.personal.PersonPresenter;
import com.yinhe.music.yhmusic.utils.SongUtil;

import java.util.ArrayList;
import java.util.List;

public class CollectDialogPresenter extends PersonPresenter<BaseModel, ICollectDialogContract.ICollectDialogView>
        implements ICollectDialogContract.ICollectDialogPresenter {

    @Override
    public void setMenuSongNum(List<SongMenuList> list) {
        if (list != null) {
            for (SongMenuList info : list) {
                MenuDBEntity menuDBEntity = DataBaseAccessor.getInstance().getMenuByIdSyn(info.getSongMenuId());
                if (menuDBEntity != null) {
                    info.setSongNum(menuDBEntity.getSongnum());
                }
            }
            getView().setCreateListUI(list);
        }
    }

    @Override
    public void addMusic(int menuId, List<Integer> songIdList) {
        if (getView() == null) {
            return;
        }

        getView().showLoading();

        //处理传入的list,分开本地和网络歌曲
        List<Integer> localList = new ArrayList<>();
        List<Integer> onlineList = new ArrayList<>();
        for (int id : songIdList) {
            if (DataBaseAccessor.getInstance().isExistSongDBId(id)) {
                localList.add(id);
            } else {
                onlineList.add(id);
            }
        }

        LogUtils.d("local:" + localList);
        LogUtils.d("online:" + onlineList);

        if (!localList.isEmpty()) {
            initData(localList, menuId);
            //只有本地歌曲时通知界面发toast
            if (onlineList.isEmpty()) {
                getView().setAddMusicUI(null);
                RxBus.get().post(RxBusEventType.SongMenu.ADD_MUSIC_TO_MENU, RxbusNullObject.INSTANCE);
            }
        }

        if (!onlineList.isEmpty()) {
            addSubscription(mModel.addSongToUserSongMenu(menuId, JsonUtil.getJsonArray(onlineList))
                    .compose(schedulerProvider.applySchedulers())
                    .subscribe(baseInfoResponse -> {
                        if (baseInfoResponse.getCode() == StatusCode.CHANGE_DATA) {
                            handAddMusic(menuId, songIdList);
                            getView().setAddMusicUI(baseInfoResponse);
                        } else {
                            getView().showToast(baseInfoResponse.getMsg());
                        }
                        getView().hideLoading();
                    }, throwable -> {
                        getView().hideLoading();
                        getView().showMessage(throwable);
                    }));
        }
    }

    @Override
    public void handleCreateResp(Response<BaseInfo> baseInfo, List<Integer> songIdList, int type) {
        super.handleCreateResp(baseInfo, songIdList, type);
        if (baseInfo.getCode() == StatusCode.REQUEST_SUCCESS) {
            MenuDBEntity menuDBEntity = new MenuDBEntity(baseInfo.getData().getSongMenuId());
            DataBaseAccessor.getInstance().insertMenu(menuDBEntity);
            addMusic(baseInfo.getData().getSongMenuId(), songIdList);
            getView().hideLoading();
        }
    }

    private void addLocalMusic(int menuId, List<Integer> songIdList, StringBuilder mLocalIdString) {
        MenuDBEntity menuDBEntity = DataBaseAccessor.getInstance().getMenuByIdSyn(menuId);
        if (menuDBEntity != null) {
            String songs = menuDBEntity.getSongIds();
            mLocalIdString.append(songs);
            menuDBEntity.setSongIds(mLocalIdString.toString());
            menuDBEntity.setSongnum(menuDBEntity.getSongnum() + songIdList.size());
            DataBaseAccessor.getInstance().updateMenu(menuDBEntity);
        }
        getView().hideLoading();

    }

    private void handAddMusic(int menuId, List<Integer> songIdList) {
        MenuDBEntity menuDBEntity = DataBaseAccessor.getInstance().getMenuByIdSyn(menuId);
        if (menuDBEntity != null) {
            menuDBEntity.setSongnum(menuDBEntity.getSongnum() + songIdList.size());
            DataBaseAccessor.getInstance().updateMenu(menuDBEntity);
        }

        RxBus.get().post(RxBusEventType.SongMenu.ADD_MUSIC_TO_MENU, RxbusNullObject.INSTANCE);
    }

    private void initData(List<Integer> songIdList, int menuId) {
        List<Integer> mLocalIds = new ArrayList<>();
        StringBuilder mLocalIdString = new StringBuilder();
        List<SongDBEntity> mSongDBList = DataBaseAccessor.getInstance().getSongListByIdArray(songIdList);

        //判重
        MenuDBEntity menuDBEntity = DataBaseAccessor.getInstance().getMenuByIdSyn(menuId);
        if (menuDBEntity == null) {
            return;
        }
        String songs = menuDBEntity.getSongIds();
        List<SongDBEntity> mList = DataBaseAccessor.getInstance().getSongListByIdArray(SongUtil.getSongIdList(songs));

        for (SongDBEntity songDBEntity : mSongDBList) {
            if (songDBEntity != null
                    && songDBEntity.isSyn()
                    && !mList.contains(songDBEntity)) {
                mLocalIds.add((int) songDBEntity.getSongid());
                mLocalIdString.append(songDBEntity.getSongid()).append(",");
            }
        }
        addLocalMusic(menuId, mLocalIds, mLocalIdString);
    }

}
